package com.qhsolution.bdict;

import com.google.analytics.tracking.android.EasyTracker;
import com.qhsolution.dbhelper.DataHelper;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class DetailActivity extends Activity {

	TextView txtDetailVi;
	TextView txtDetailEng;
	TextView txtDetailCategory;
	TextView txtDetailContent;

	ImageButton btnDetailFeatured;

	public static final String KEY_ID = "_id";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		txtDetailVi = (TextView) findViewById(R.id.txtDetailVi);
		txtDetailEng = (TextView) findViewById(R.id.txtDetailEng);
		txtDetailCategory = (TextView) findViewById(R.id.txtDetailCategory);
		txtDetailContent = (TextView) findViewById(R.id.txtDetailContent);

		btnDetailFeatured = (ImageButton) findViewById(R.id.imgDetailFeatured);

		Intent intent = getIntent();
		String Id = intent.getStringExtra(MainActivity.WORD_ID);
		DataHelper dbHelper = new DataHelper(getApplicationContext());
		dbHelper.openDataBase();
		final Cursor c = dbHelper.getSelect("word", null, KEY_ID + " = ?",
				new String[] { Id }, null, null, null);
		if (c.getCount() > 0) {
			c.moveToFirst();
			String name = "";
			if (c.getString(1) != null) {
				name = c.getString(1);
				name = name.trim();
			}
			String eng = "";
			if (c.getString(7) != null) {
				eng = c.getString(7);
				eng = eng.trim();
			}

			String summary = "";
			if (c.getString(3) != null) {
				summary = c.getString(3);
				summary = summary.trim();
			}
			String category = "";
			// Get category
			Cursor cCategory = dbHelper
					.getSelect("category", null, KEY_ID + " = ?",
							new String[] { c.getString(6) }, null, null, null);
			if (cCategory.getCount() > 0) {
				cCategory.moveToFirst();
				category = cCategory.getString(1);
			}
			getActionBar().setTitle(name);
			txtDetailVi.setText(name);
			txtDetailEng.setText(eng);
			txtDetailCategory.setText(category);
			txtDetailContent.setText(summary);
			Log.d("FeaturedValue", c.getString(5));
			if (Integer.parseInt(c.getString(5)) == 1) {
				btnDetailFeatured.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_action_important));
			} else {
				btnDetailFeatured.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_action_not_important));
			}
			btnDetailFeatured.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					DataHelper dbHelper = new DataHelper(
							getApplicationContext());
					dbHelper.openDataBase();
					ContentValues value = new ContentValues();
					if (Integer.parseInt(c.getString(5)) == 1) {
						value.put("featured", "0");
						btnDetailFeatured
								.setImageResource(R.drawable.ic_action_not_important);
					} else {
						value.put("featured", "1");
						btnDetailFeatured
								.setImageResource(R.drawable.ic_action_important);
					}
					dbHelper.updateItems("word", value, KEY_ID + " = ? ",
							new String[] { c.getString(0) });
					dbHelper.close();
				}
			});
		}
		dbHelper.close();

		getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
//		case android.R.id.home:
//			// go to previous screen when app icon in action bar is clicked
//			Intent intent = new Intent(this, MainActivity.class);
//			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			startActivity(intent);
//			this.finish();
//			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
	 }

	 @Override
	 public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	 }	 
}
